package shuashuami.hb.com.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import shuashuami.hb.com.avtivity.CHallInfoActivity;
import shuashuami.hb.com.avtivity.CQuerenInfoActivity;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.glid.GlidUtil;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.model.CUntreaterTask;

/**
 * Created by Administrator on 2017/6/20.
 */

public class CUntreadeterTaskAadpter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<CUntreaterTask> list;
    private Context con;

    private GlidUtil glidUtil;

    public CUntreadeterTaskAadpter(List<CUntreaterTask> list, Context context) {
        this.con = context;
        inflater = LayoutInflater.from(context);
        this.list = list;
        glidUtil = new GlidUtil(context);
    }

    @Override
    public int getCount() {
        if (null != list) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CdistriutionCenterViewHoler viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_cuntreater_task_layout, null);
            viewHolder = new CdistriutionCenterViewHoler();
            viewHolder.imgP = (ImageView) convertView.findViewById(R.id.img_cuntreater_task_p_image);
            viewHolder.tvNum = (TextView) convertView.findViewById(R.id.tv_cuntreater_task_task_number);
            viewHolder.tvStatus = (TextView) convertView.findViewById(R.id.tv_cuntreater_task_task_status);
            viewHolder.imgImage = (ImageView) convertView.findViewById(R.id.img_cuntreater_task_task_image);
            viewHolder.tvMessage = (TextView) convertView.findViewById(R.id.tv_cuntreater_task_task_message);
            viewHolder.tvMoney = (TextView) convertView.findViewById(R.id.tv_cuntreater_task_task_money);
            viewHolder.tvSize = (TextView) convertView.findViewById(R.id.tv_cuntreater_task_task_size);
            viewHolder.tvTotal = (TextView) convertView.findViewById(R.id.tv_cuntreater_task_task_total);
            viewHolder.tvEndTime = (TextView) convertView.findViewById(R.id.tv_cuntreater_task_task_end_time);
            viewHolder.tvYOngjin = (TextView) convertView.findViewById(R.id.tv_cuntreater_task_yongjin);
            viewHolder.btnLianxi = (Button) convertView.findViewById(R.id.btn_cuntreater_task_task_lianxi_shangjia);
            viewHolder.btnXiangqing = (Button) convertView.findViewById(R.id.btn_cuntreater_task_task_chakan_xiangqing);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (CdistriutionCenterViewHoler) convertView.getTag();
        }
        final CUntreaterTask content = list.get(position);
        glidUtil.uploadRound(content.getpImage(), 10, viewHolder.imgP);
        glidUtil.uploadRound(content.getTImage(), 10, viewHolder.imgImage);
        viewHolder.tvNum.setText("任务编号：" + content.getNum());
         String status = content.getStatus();
        if (status.equals("1")) {
            status = "待接单";
        } else if (status.equals("2")) {
            status = "待提交";
        } else if (status.equals("3")) {
            status = "已提交，待审核";
        } else if (status.equals("4")) {
            status = "待发货";
        } else if (status.equals("5")) {
            status = "待收货";
        } else if (status.equals("6")) {
            status = "已收货";
        } else if (status.equals("7")) {
            status = "已评价";
        } else if (status.equals("8")) {
            status = "已作废";
        } else if (status.equals("9")) {
            status = "已驳回";
        } else if (status.equals("10")) {
            status = "已关闭";
        } else if (status.equals("20")) {
            status = "已取消";
        }
        viewHolder.tvStatus.setText(status);
        viewHolder.tvMessage.setText(content.getMessage());
        viewHolder.tvMoney.setText("垫付资金 ¥ " + content.getMoney());
        viewHolder.tvYOngjin.setText("佣金 ¥ " + content.getYongjin());
        viewHolder.tvSize.setText("X " + content.getSize());
        viewHolder.tvTotal.setText("¥ " + content.getTotal());
        viewHolder.tvEndTime.setText(content.getEndTime());
        viewHolder.btnXiangqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;
                Bundle bundle = new Bundle();
        /*字符、字符串、布尔、字节数组、浮点数等等，都可以传*/
                if (content.getStatus().equals("5")) {
                    intent = new Intent(con, CQuerenInfoActivity.class);
                } else if (content.getStatus().equals("3")) {
                    bundle.putString("status", content.getStatus());
                    intent = new Intent(con, CQuerenInfoActivity.class);
                } else if (content.getStatus().equals("4")) {
                    intent = new Intent(con, CQuerenInfoActivity.class);
                } else if (content.getStatus().equals("6")) {
                    intent = new Intent(con, CQuerenInfoActivity.class);
                } else if (content.getStatus().equals("7")) {
                    bundle.putString("status", content.getStatus());
                    intent = new Intent(con, CQuerenInfoActivity.class);
                } else {
                    intent = new Intent(con, CHallInfoActivity.class);
                }


                bundle.putString("id", content.getId());

                //    bundle.putString("num", content.getNum());

		/*把bundle对象assign给Intent*/
                intent.putExtras(bundle);
                con.startActivity(intent);
            }
        });
        viewHolder.btnLianxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toQQ(content.getQq());
            }
        });
        return convertView;
    }

    private void toQQ(String qq) {
        try {
            //第二种方式：可以跳转到添加好友，如果qq号是好友了，直接聊天
            String url = "mqqwpa://im/chat?chat_type=wpa&uin=" + qq;//uin是发送过去的qq号码
            con.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.showShort(con, "请检查是否安装QQ");
        }
    }

    class CdistriutionCenterViewHoler {
        public ImageView imgP;
        public TextView tvNum;
        public TextView tvStatus;
        public ImageView imgImage;
        public TextView tvMessage;
        public TextView tvMoney;
        public TextView tvSize;
        public TextView tvTotal;
        public TextView tvEndTime;
        public Button btnLianxi, btnXiangqing;
        public TextView tvYOngjin;
    }


}



